package boxync;

import java.io.File;
import java.util.Date;

public abstract class FileInfo implements Comparable<FileInfo> {
    /**
     * File yang ada di harddisk
     */
    public String file;
    
    /**
     * Nama file
     */
    public String name;
    
    /**
     * Date terakhir file dimodif
     */
    public Date timeStamp;
    
    /**
     * Directory parent di dalam repo
     */
    public String parent;
    
    /**
     * Mark File sudah didelete
     */
    public byte deleted;         // Mark udah didelete
    
    /**
     * 
     * @return file name (termasuk direktori, namun tidak termasuk repoDir) dari String yg merepresentasikan fileInfo
     */
    public String getFileName() {
        return parent + "/" + name;
    }
    
    /**
     * Mengembalikan String parent.
     * @return String yg merupakan direktori dari fileInfo ini
     */
    public String getParent() {
        return getFileName().substring(0, getFileName().lastIndexOf('/'));
    }
    
    public abstract void delete();
    
    public static boolean isDirectory(String fileName) {
        int pointIdx = fileName.indexOf('.', fileName.lastIndexOf('/'));
        if (pointIdx < 0)
            return true;
        
        String ext = fileName.substring(pointIdx + 1);
        return (ext.length() < 3 || ext.length() > 4);
    }
    
    /**
     * @param fileInfo String yg merepresentasikan fileInfo
     * @return file name (termasuk direktori, namun tidak termasuk repoDir) dari String yg merepresentasikan fileInfo
     */
    public static String getFileName(String fileInfo) {
        return fileInfo.substring(0, fileInfo.indexOf('#'));
    }
    
    /**
     * Mengembalikan String parent dari String fileInfo.
     * @param fileInfo String yg ingin diketahui parentnya
     * @return String yg merupakan direktori dari fileInfo
     */
    public static String getParent(String fileInfo) {
        return fileInfo.substring(0, FileInfo.getFileName(fileInfo).lastIndexOf('/'));
    }
    
    /**
     * 
     * @param fileInfo
     * @return String timeStamp dari String fileInfo
     */
    public static String getTimeStamp(String fileInfo) {
        int firstPound = fileInfo.indexOf('#');
        return fileInfo.substring(firstPound + 1, fileInfo.indexOf('#', firstPound+1));
    }
    
    /**
     * 
     * @param time
     * @return Tahun dari String timeStamp
     */
    public static int getYear(String time) {
        return Integer.parseInt(time.substring(0, 4));
    }
    
    /**
     * 
     * @param time
     * @return Bulan dari String timeStamp - 1
     */
    public static int getMonth(String time) {
        return Integer.parseInt(time.substring(5, 7));
    }
    
    /**
     * 
     * @param time
     * @return Tanggal dari String timeStamp
     */
    public static int getDate(String time) {
        return Integer.parseInt(time.substring(8, 10));
    }
    
    /**
     * 
     * @param time
     * @return Jam dari String timeStamp
     */
    public static int getHour(String time) {
        return Integer.parseInt(time.substring(11, 13));
    }
    
    /**
     * 
     * @param time
     * @return Menit dari String timeStamp
     */
    public static int getMinute(String time) {
        return Integer.parseInt(time.substring(14, 16));
    }
    
    /**
     * 
     * @param time
     * @return Detik dari String timeStamp
     */
    public static int getSecond(String time) {
        return Integer.parseInt(time.substring(17, 19));
    }
    
    /**
     * 
     * @param fileInfo BoxyncFile yg ingin diperiksa
     * @return true jika fileInfo sudah dimark delete, yaitu karakter terakhirnya = 1
     */
    public static boolean isDeleted(String fileInfo) {
        return fileInfo.charAt(fileInfo.length()-1) == '1';
    }
    
    /**
     * 
     * @return String timeStamp dengan format YYYY/MM/DD hh:mm:ss
     */
    public String timeStampString() {
        return timeStamp.getYear() + "/" 
                + (timeStamp.getMonth() < 9 ? "0" + (timeStamp.getMonth()+1) : timeStamp.getMonth()+1) + "/"
                + (timeStamp.getDate() < 10 ? "0"+timeStamp.getDate() : timeStamp.getDate()) + " "
                + (timeStamp.getHours() < 10 ? "0" + timeStamp.getHours() : timeStamp.getHours()) + ":"
                + (timeStamp.getMinutes() < 10 ? "0" + timeStamp.getMinutes() : timeStamp.getMinutes()) + ":"
                + (timeStamp.getSeconds() < 10 ? "0" + timeStamp.getSeconds() : timeStamp.getSeconds());
    }
    
    /**
     * 
     * @param o BoxyncFile yg ingin dibandingkan
     * @return true jika getFileName() sama
     */
    @Override
    public boolean equals(Object o) {
        FileInfo fi = (FileInfo)o;
        
        if (fi == null)
            return false;
        
        return getFileName().equals(fi.getFileName());
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 37 * hash + (this.file != null ? this.file.hashCode() : 0);
        hash = 37 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 37 * hash + (this.timeStamp != null ? this.timeStamp.hashCode() : 0);
        hash = 37 * hash + (this.parent != null ? this.parent.hashCode() : 0);
        hash = 37 * hash + this.deleted;
        return hash;
    }

    /**
     * 
     * @param fi BoxyncFile yg ingin dibandingkan
     * @return int positif jika this.timeStamp > fi.timeStamp, 
     * 0 jika this.timeStamp == fi.timeStamp, dan
     * negatif jika this.timeStamp < fi.timeStamp
     */
    @Override
    public int compareTo(FileInfo fi) {
        Date timeStamp2 = fi.timeStamp;
        if (timeStamp.getYear() != timeStamp2.getYear()) {
            if (timeStamp.getYear() < timeStamp2.getYear())
                return -1;
            return 1;
        }
        
        if (timeStamp.getMonth() != timeStamp2.getMonth()) {
            if (timeStamp.getMonth() < timeStamp2.getMonth())
                return -1;
            return 1;
        }
        
        if (timeStamp.getDate() != timeStamp2.getDate()) {
            if (timeStamp.getDate() < timeStamp2.getDate())
                return -1;
            return 1;
        }
        
        if (timeStamp.getHours() != timeStamp2.getHours()) {
            if (timeStamp.getHours() < timeStamp2.getHours())
                return -1;
            return 1;
        }
        
        if (timeStamp.getMinutes() != timeStamp2.getMinutes()) {
            if (timeStamp.getMinutes() < timeStamp2.getMinutes())
                return -1;
            return 1;
        }
        
        if (timeStamp.getSeconds() < timeStamp2.getSeconds())
            return -1;
        if (timeStamp.getSeconds() > timeStamp2.getSeconds())
            return 1;
        
        return 0;
    }
    
    public int compareTo(String fi) {
        String time = FileInfo.getTimeStamp(fi);
        
        int year = FileInfo.getYear(time);
        if (timeStamp.getYear() != year) {
            if (timeStamp.getYear() < year)
                return -1;
            return 1;
        }
        
        int month = FileInfo.getMonth(time);
        if (timeStamp.getMonth() + 1 != month) {
            if (timeStamp.getMonth() + 1 < month)
                return -1;
            return 1;
        }
        
        int day = FileInfo.getDate(time);
        if (timeStamp.getDate() != day) {
            if (timeStamp.getDate() < day)
                return -1;
            return 1;
        }
        
        int hour = FileInfo.getHour(time);
        if (timeStamp.getHours() != hour) {
            if (timeStamp.getHours() < hour)
                return -1;
            return 1;
        }
        
        int minute = FileInfo.getMinute(time);
        if (timeStamp.getMinutes() != minute) {
            if (timeStamp.getMinutes() < minute)
                return -1;
            return 1;
        }
        
        int second = FileInfo.getSecond(time);
        if (timeStamp.getSeconds() < second)
            return -1;
        if (timeStamp.getSeconds() > second)
            return 1;
        return 0;
    }
    
    public static void main(String[] args) {
        File f = new File("C:/boxync/wibi");
        f.setLastModified(new Date(1200).getTime());
    }
}
